<template>
  <j-modal
    :title="$t('结账单')"
    :width="width"
    :visible="visible"
    switchFullscreen
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
  >
    <template slot="footer">
      <a-button type="primary" @click="handleOk">{{$t('结账')}}</a-button>
      <a-button @click="handleCancel">{{$t('关闭')}}</a-button>
    </template>
    <div>
      <a-row :gutter="[10, 20]">
        <a-col :span="10" :offset="1">
          <div>{{$t('账目开始时间')}}：{{ model.startTime }}</div>
        </a-col>
         <a-col :span="8">
          <div>{{$t('商家')}}：{{ model.supplierName }}（{{ model.supplierCode }}）</div>
        </a-col>
      </a-row>
      <a-row :gutter="[0, 30]">
        <a-col>
          <ul>
            <li style="font-weight: bold">{{$t('商品销售明细')}}</li>
          </ul>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="22" :offset="1">
          <a-table :columns="columns5" :dataSource="dataSource5" :pagination="false" bordered></a-table>
        </a-col>
      </a-row>
      <a-row :gutter="[0, 30]">
        <a-col>
          <ul>
            <li style="font-weight: bold">{{$t('扣费明细')}}</li>
          </ul>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="22" :offset="1">
          <a-table :columns="columns6" :dataSource="dataSource6" :pagination="false" bordered></a-table>
        </a-col>
      </a-row>
    </div>
  </j-modal>
</template>

<script>
import { getAction,postAction } from '@/api/manage'
export default {
  name: 'NotDiffCheckOutModal',
  components: {
  },
  data() {
    return {
      width: 800,
      visible: false,
      disableSubmit: false,
      model: {},
      columns5: [
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsName',
        },
        {
          title: this.$t('数量'),
          align: 'center',
          dataIndex: 'amount',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('金额'),
          align: 'center',
          dataIndex: 'cash',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
        }
      ],
      columns6: [
        {
          title: this.$t('扣费项目'),
          align: 'center',
          dataIndex: 'payType_dictText',
        },
        {
          title: this.$t('业务笔数'),
          align: 'center',
          dataIndex: 'amount',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('扣费值'),
          align: 'center',
          dataIndex: 'deductFee',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
      ],
      dataSource5: [],
      dataSource6: [],
    }
  },
  methods: {
    edit(record) {
      this.columns5=[
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsName',
        },
        {
          title: this.$t('数量'),
          align: 'center',
          dataIndex: 'amount',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('金额'),
          align: 'center',
          dataIndex: 'cash',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('结算价'),
          align: 'center',
          dataIndex: 'cost',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        }
      ]
      if (localStorage.getItem('operateCoin') == 1) {
        this.columns5.splice(this.columns5.length - 1, 0, {
          title: this.$t('扣币'),
          align: 'center',
          dataIndex: 'coin',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        })
      }
      if (localStorage.getItem('operateLottery') == 1) {
        this.columns5.splice(this.columns5.length - 1, 0, {
          title: this.$t('扣票'),
          align: 'center',
          dataIndex: 'lottery',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateALottery') == 1) {
        this.columns5.splice(this.columns5.length - 1, 0, {
          title: this.$t('扣A票'),
          align: 'center',
          dataIndex: 'lotteryA',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      if (localStorage.getItem('operateACoin') == 1) {
        this.columns5.splice(this.columns5.length - 1, 0, {
          title: this.$t('扣A币'),
          align: 'center',
          dataIndex: 'coinA',
          customCell: function () {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        })
      }
      this.dataSource5 = []
      this.dataSource6 = []
      this.visible = true
      this.model = Object.assign({}, record)
      getAction('/diffcheckout/notDiffCheckOut/queryNotDiffCheckOutGoods', {
        supplierId: this.model.supplierId
      }).then((res) => {
        if (res.success) {
          this.dataSource5 = res.result
          if (this.dataSource5.length > 0) {
            this.addSumRow(['goodsName', 'amount', 'cash', 'coin', 'lottery','coinA','lotteryA', 'cost'], this.dataSource5)
          }
        }
      })
      getAction('/diffcheckout/notDiffCheckOut/queryNotDiffCheckOutDeduct', {
        supplierId: this.model.supplierId
      }).then((res) => {
        if (res.success) {
          this.dataSource6 = res.result
        }
      })
    },
    addSumRow(myArr, dataSource) {
      let total = {}
      myArr.forEach((tcn, ind)=> {
        if (ind == 0) {
          total[tcn] = this.$t('合计')
        } else {
          total[tcn] = 0
        }
      })
      for (let key in total) {
        dataSource.forEach(function (ds) {
          if (!isNaN(total[key])) {
            total[key] += Number(ds[key])
          }
        })
      }
      dataSource.push(total)
    },
    handleOk() {
      let that = this
      let query = Object.assign({}, that.model)
      query.bisDiffCheckOutGoodsList = [];
      that.dataSource5.forEach(function (value, index) {
        if(index != that.dataSource5.length-1)
          query.bisDiffCheckOutGoodsList.push(value)
      });
      query.bisDiffCheckOutDeductList = [];
      query.bisDiffCheckOutDeductList.push(...that.dataSource6)
      that.$confirm({
        content: that.$t('是否进行结账操作？'),
        okText: that.$t("确定"),
        cancelText: that.$t("取消"),
        onOk: () => {
          console.log(query)
          postAction('/diffcheckout/bisDiffCheckOut/add', query).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          })
        },
      })
    },
    close() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>